/*******************************************************************************

Copyright (c) 2003, Robert Cowham and Vaccaperna Systems Ltd.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1.  Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

2.  Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL VACCAPERNA SYSTEMS LTD. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

/*******************************************************************************
 * Name:	p4clientapi.h
 *
 * Author:	Robert Cowham <robert@vaccaperna.co.uk>
 *
 * Description: 
 *		COM bindings for the Perforce API. User interface class
 * 		for getting Perforce results into COM.
 *
 ******************************************************************************/

#ifndef _P4API_H_
#define _P4API_H_

// P4 API files
# include "PerforceAPI.h"
# include "enviro.h"
# include "signaler.h"

# include "msgclient.h"
# include "spec.h"

#endif // _P4API_H_

#include "p4clientuser.h"

#ifdef SetPort		// Windows bug
#undef SetPort
#endif

class p4ClientApi {

public:
	// caller's main interface

	p4ClientApi();
	~p4ClientApi();

	    // Protocol options. Call before Connect()
    void Tagged();
    void ParseForms();

    // Session management
    int Connect(StrBuf &errMsg);
    int Disconnect();

	void  ExceptionLevel( int i )	{ m_exceptionLevel = i; }
    int   ExceptionLevel()		{ return m_exceptionLevel; }

	long Run(BSTR cmd, StrBuf &errMsg);
	StrBuf ErrorMsg();
	void CheckCharset();

	void SetCharset(BSTR s)
	{
		m_client.SetCharset(ConvertString(s));
	}
	void SetClient(BSTR s)
	{
		m_client.SetClient(ConvertString(s));
	}
	void SetCwd(BSTR s)
	{
		m_client.SetCwd(ConvertString(s));
	}
	void SetHost(BSTR s)
	{
		m_client.SetHost(ConvertString(s));
	}
	void SetLanguage(BSTR s)
	{
		m_client.SetLanguage(ConvertString(s));
	}
	void SetPassword(BSTR s)
	{
		m_client.SetPassword(ConvertString(s));
	}
	void SetPort(BSTR s)
	{
		m_client.SetPort(ConvertString(s));
	}
	void SetUser(BSTR s)
	{
		m_client.SetUser(ConvertString(s));
	}
	void SetInput(BSTR s)
	{
		m_ui.SetInput(ConvertString(s));
	}


	LPCSTR GetCharset() 
	{
		return m_client.GetCharset().Text();
	}
	LPCSTR GetClient() 
	{
		return m_client.GetClient().Text();
	}
	LPCSTR GetCwd()
	{
		return m_client.GetCwd().Text();
	}
	LPCSTR GetHost()
	{
		return m_client.GetHost().Text();
	}
	LPCSTR GetLanguage()
	{
		return m_client.GetLanguage().Text();
	}
	LPCSTR GetOs()
	{
		return m_client.GetOs().Text();
	}
	LPCSTR GetPassword()
	{
		return m_client.GetPassword().Text();
	}
	LPCSTR GetPort()
	{
		return m_client.GetPort().Text();
	}
	LPCSTR GetUser()
	{
		return m_client.GetUser().Text();
	}
	const long GetServerVersion()
	{
		StrPtr * sp = m_client.GetProtocol("server2");
		long result = sp->Atoi();
		return result;
	}
	int Dropped()
	{
		return m_client.Dropped();
	}


public:
	p4ClientUser m_ui;

private:
	ClientApi m_client;
    int m_initCount;
    int m_exceptionLevel;
    int	m_server2;
    int	m_tagged;
	StrBuf m_emptyBuf;
	StrBuf m_buf;

	void p4ClientApi::SplitArgs(BSTR cmd_line, StrBufArray *sba);

private:
	StrBuf *ConvertString(BSTR s)
	{
		if (NULL == s)
		{
			return &m_emptyBuf;
		}
		else
		{
			m_buf = (LPCSTR)CW2A(s);
			return &m_buf;
		}
	}

} ;
